#===============================================================================
# Copyright 2005-2017 Intel Corporation All Rights Reserved.
#
# The source code,  information  and material  ("Material") contained  herein is
# owned by Intel Corporation or its  suppliers or licensors,  and  title to such
# Material remains with Intel  Corporation or its  suppliers or  licensors.  The
# Material  contains  proprietary  information  of  Intel or  its suppliers  and
# licensors.  The Material is protected by  worldwide copyright  laws and treaty
# provisions.  No part  of  the  Material   may  be  used,  copied,  reproduced,
# modified, published,  uploaded, posted, transmitted,  distributed or disclosed
# in any way without Intel's prior express written permission.  No license under
# any patent,  copyright or other  intellectual property rights  in the Material
# is granted to  or  conferred  upon  you,  either   expressly,  by implication,
# inducement,  estoppel  or  otherwise.  Any  license   under such  intellectual
# property rights must be express and approved by Intel in writing.
#
# Unless otherwise agreed by Intel in writing,  you may not remove or alter this
# notice or  any  other  notice   embedded  in  Materials  by  Intel  or Intel's
# suppliers or licensors in any way.
#===============================================================================

##  Content:
##      Intel(R) Math Kernel Library BLAS95 examples creation and run
##******************************************************************************

help:
	@echo Usage: nmake /f win.mak {libia32^|dllia32^|libintel64^|dllintel64^} {function=name+}
	@echo [interface=interface_name] [threading=threading_name]
	@echo [parallel=parallel_name] [omp=omp_name]
	@echo [F95ROOT=f95_root_path] [MKLROOT=MKL_root_path]
	@echo name           - function name.
	@echo interface_name - can be lp64 or ilp64 for intel64. Default value is lp64.
	@echo threading_name - can be parallel or sequential. Default value is parallel.
	@echo parallel_name  - can be intel or pg (only if compiler=pgi). Default value is intel.
	@echo omp_name       - can be iomp5.
	@echo f95_root_path  - Path to blas95 library and .mod files that are built on user side.
	@echo                  Mandatory if compiler is not intel. To build library and .mod files,
	@echo                  go to the directory MKLROOT\interfaces\blas95 and run:
	@echo                  nmake target [options] FC=f95compiler install_dir=f95_root_path
	@echo                  See description of the FC and install_dir in the interface makefile.
	@echo MKL_root_path  - Locate Intel(R) MKL libraries relative to MKL_root_path.
	@echo                  Default value: ..\.., unless defined in environment.

##------------------------------------------------------------------------------
## examples of using:
##
## nmake /f win.mak dllintel64 function=example_MySparseOperator+
##------------------------------------------------------------------------------

compiler = intel

!IFNDEF interface
interface = lp64
!ENDIF

!IFNDEF threading
threading = parallel
!ENDIF

!IFNDEF parallel
parallel = intel
!ENDIF

!IFNDEF omp
omp = iomp5
!ENDIF

!IFNDEF MKLROOT
MKLROOT = "D:\Program Files (x86)\IntelSWTools\compilers_and_libraries\windows\mkl\"
!ENDIF

RES = $(function:+=.res)


IFACE_COMP_PART=intel
IFACE_THREADING_PART=intel
FC=ifort
OUT_O=-Fo

!IF ("$(interface)"=="ilp64")
IFACE_LIB=mkl_$(IFACE_COMP_PART)_ilp64$(EXT)
FOPTS = /4I8
!ELSE
IFACE_LIB=mkl_$(IFACE_COMP_PART)_lp64$(EXT)
FOPTS =
!ENDIF

!IF ("$(_IA)" == "ia32")
IFACE_LIB=mkl_$(IFACE_COMP_PART)_c$(EXT)
FOPTS =
!ENDIF

!IF ("$(threading)"=="sequential")
THREADING_LIB=mkl_sequential$(EXT)
OMP_LIB =
OMP_EXT =
!ELSE
THREADING_LIB=mkl_$(IFACE_THREADING_PART)_thread$(EXT)
OMP_LIB = libiomp5md.lib
OMP_EXT = _iomp5
!ENDIF

CORE_LIB=mkl_core$(EXT)

MKL_LIBS=$(IFACE_LIB) $(THREADING_LIB) $(CORE_LIB) $(OMP_LIB)

!IFNDEF RES_DIR
RES_DIR=.
!ENDIF

FOPTS = $(FOPTS) /w /MT
OUT= /Fo$(RES_DIR)\ /Fe$(RES_DIR)\ #

!IFNDEF F95ROOT
F95ROOT = $(MKLROOT)
!IF ("$(compiler)"!="intel")
!ERROR *** F95ROOT is mandatory parameter for $(compiler)
!ENDIF
!ENDIF

!IF ("$(_IA)" == "ia32")
LIB95=mkl_blas95.lib
!ELSE
LIB95=mkl_blas95_$(interface).lib
!ENDIF

SPEC_OPT = /fpp

libia32 lib32:
	set lib=$(F95ROOT)\lib\ia32;$(MKLROOT)\lib\ia32;$(MKLROOT)\..\compiler\lib\ia32;%%lib%%
	set path=$(MKLROOT)\..\redist\ia32\mkl;$(MKLROOT)\..\redist\ia32\compiler;%%path%%
	set include=$(F95ROOT)\include\ia32;%%include%%
	set pgf95_module_path=$(F95ROOT)\include\ia32
	nmake comm_func $(RES) del_objs EXT=.lib _IA=ia32 RES_EXT=lib interface=$(interface) compiler=$(compiler) threading=$(threading) parallel=$(parallel) omp=$(omp)
dllia32 dll32:
	set lib=$(F95ROOT)\lib\ia32;$(MKLROOT)\lib\ia32;$(MKLROOT)\..\compiler\lib\ia32;%%lib%%
	set path=$(MKLROOT)\..\redist\ia32\mkl;$(MKLROOT)\..\redist\ia32\compiler;%%path%%
	set include=$(F95ROOT)\include\ia32;%%include%%
	set pgf95_module_path=$(F95ROOT)\include\ia32
	nmake comm_func $(RES) del_objs EXT=_dll.lib _IA=ia32 RES_EXT=dll interface=$(interface) compiler=$(compiler) threading=$(threading) parallel=$(parallel) omp=$(omp)

libintel64 libem64t:
	set lib=$(F95ROOT)\lib\intel64;$(MKLROOT)\lib\intel64;$(MKLROOT)\..\compiler\lib\intel64;%%lib%%
	set path=$(MKLROOT)\..\redist\intel64\mkl;$(MKLROOT)\..\redist\intel64\compiler;%%path%%
	set include=$(F95ROOT)\include\intel64\$(interface);%%include%%
	set pgf95_module_path=$(F95ROOT)\include\intel64\$(interface)
	nmake comm_func $(RES) del_objs EXT=.lib _IA=intel64 RES_EXT=lib interface=$(interface) compiler=$(compiler) threading=$(threading) parallel=$(parallel) omp=$(omp)
dllintel64 dllem64t:
	set lib=$(F95ROOT)\lib\intel64;$(MKLROOT)\lib\intel64;$(MKLROOT)\..\compiler\lib\intel64;%%lib%%
	set path=$(MKLROOT)\..\redist\intel64\mkl;$(MKLROOT)\..\redist\intel64\compiler;%%path%%
	set include=$(F95ROOT)\include\intel64\$(interface);$(F95ROOT)\include;%%include%%
	set pgf95_module_path=$(F95ROOT)\include\intel64\$(interface)
	if not exist $(RES_DIR) md $(RES_DIR)
	nmake /f win.mak $(RES) EXT=_dll.lib _IA=intel64 RES_EXT=dll interface=$(interface) compiler=$(compiler) threading=$(threading) parallel=$(parallel) omp=$(omp)

#-------------------------------------------------------------------------------



.SUFFIXES:: .f90 .res

example_d_MySparseOperator.res:
	$(FC) $(FOPTS) /c /module:"$(RES_DIR)" /Fo"$(RES_DIR)"\ common_functions.f90
	$(FC) $(FOPTS) /c /module:"$(RES_DIR)" /Fo"$(RES_DIR)"\ MySparseOperator.f90
    $(FC) $(FOPTS) /module:"$(RES_DIR)" $(OUT) example_d_MySparseOperator.f90 common_functions.obj MySparseOperator.obj $(LIB95) $(MKL_LIBS)

example_z_MySparseOperator.res:
	$(FC) $(FOPTS) /c /module:"$(RES_DIR)" /Fo"$(RES_DIR)"\ common_functions.f90
	$(FC) $(FOPTS) /c /module:"$(RES_DIR)" /Fo"$(RES_DIR)"\ MySparseOperator.f90
    $(FC) $(FOPTS) /module:"$(RES_DIR)" $(OUT) example_z_MySparseOperator.f90 common_functions.obj MySparseOperator.obj $(LIB95) $(MKL_LIBS)
    ./example_z_MySparseOperator.exe
# $(RES_DIR)\$(@B).exe > $(RES_DIR)\$@

example_FEM.res:
	if not exist "mkl_pardiso.mod mkl_pardiso_private.mod" $(FC) $(FOPTS) /c /module:"$(RES_DIR)" /Fo"$(RES_DIR)"\ "$(MKLROOT)\include\mkl_pardiso.f90"
	$(FC) $(FOPTS) /c /module:"$(RES_DIR)" /Fo"$(RES_DIR)"\ "$(MKLROOT)\..\compiler\include\ifport.f90"
	$(FC) $(FOPTS) /c /module:"$(RES_DIR)" /Fo"$(RES_DIR)"\ common_functions.f90
	$(FC) $(FOPTS) /c /module:"$(RES_DIR)" /Fo"$(RES_DIR)"\ MySparseOperator.f90
	$(FC) $(FOPTS) /c /module:"$(RES_DIR)" /Fo"$(RES_DIR)"\ myFEM.f90
    $(FC) $(FOPTS) /module:"$(RES_DIR)" $(OUT) example_FEM.f90 common_functions.obj myfem.obj MySparseOperator.obj $(LIB95) $(MKL_LIBS)


del_objs:
	del /Q $(RES_DIR)\*.obj

#-------------------------------------------------------------------------------
